package net.lainiao.mycost.controller;

import net.lainiao.mycost.bean.MenuInfo;
import net.lainiao.mycost.bean.RoleInfo;
import net.lainiao.mycost.bean.RoleMenuInfo;
import net.lainiao.mycost.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2017/3/22.
 */
@Controller
@RequestMapping("/anthority/")
public class AnthorityController extends Page {

    @RequestMapping("list.do")
    public String list(RoleInfo info, Model model, HttpServletRequest request){
        if(info==null){
            info=new RoleInfo();
        }
        this.initPage(request);
        info.setStart(this.getPageNo());
        info.setLength(PAGE_NUM_BIG);
        model.addAttribute("list",roleInfoService.list(info));
        model.addAttribute("total",roleInfoService.getCount(info));
        return "anthority/anthority_list";
    }

    @RequestMapping("loadchange.do")
    public String loadChange(RoleInfo info,Model model){
        RoleInfo role=roleInfoService.getInfo(info);
        model.addAttribute("role",role);
        List<MenuInfo> list=menuInfoService.list(null);
        model.addAttribute("list",list);

        RoleMenuInfo rminfo=new RoleMenuInfo();
        rminfo.setRoleId(info.getRoleId());
        List<RoleMenuInfo> rmlist=anthorityService.list(rminfo);
        model.addAttribute("rmlist",rmlist);
        return "anthority/anthority_change";
    }

    @RequestMapping("change.do")
    public String change(Integer roleId,Integer[] menuIds,Model model,HttpServletRequest request){
        anthorityService.add(roleId,menuIds);
        return list(null,model,request);
    }

}
